package web;

import ejbEntity.Album;
import ejbEntity.Artista;
import ejbEntity.Cancion;
import ejbSessionBeans.AlbumFacadeLocal;
import ejbSessionBeans.ArtistaFacadeLocal;
import ejbSessionBeans.CancionFacadeLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author bbaquero
 */
public class MostrarAlbum extends HttpServlet {
    @EJB
    private CancionFacadeLocal cancionFacade;

    @EJB
    private ArtistaFacadeLocal artistaFacade;
    @EJB
    private AlbumFacadeLocal albumFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            out.println("<html>");
            out.println("<head>");
            String art = request.getParameter("arti");
            Artista arti = artistaFacade.find(new Integer(art));
            out.println("<title>))) RAW ((( Albumes de "+ arti.getNombre() +"</title>");
            out.println("<link href=\"styleRAW.css\" rel=\"stylesheet\" type=\"text/css\" /></head>");
            out.println("<body>");
            out.println(Resources.setSideBar()); //Barra de navegacion de seccion Rock.
            
            out.println("<div id=content><div id=infobar>" +
                    "<center><font color=white size=2><b>Artista: " + arti.getNombre() + "</b></font> </center></div>");
            List<Album> album = albumFacade.findByIdArtista(new Integer(art));
            out.println("<br><br><br>");
            for (Iterator it = album.iterator(); it.hasNext();) {
                Album elem = (Album) it.next();
                out.println("<font color=grey size=2 face=verdana>Titulo: </font> <a href=MostrarCanciones?album=" + elem.getIdAlbum() + "&arti=" + arti.getIdArtista() + "><font color=white size=3 face=verdana>" + elem.getTitulo() + "</font></a><font color=white size=2 face=verdana> (" + elem.getYear() + ")</font><br />");
                
                List<Cancion> canc = cancionFacade.findByAlbum(elem.getIdAlbum());

                for (Iterator its = canc.iterator(); its.hasNext();) {
                    Cancion eleme = (Cancion) its.next();
                    out.println("<a href=MostrarCanciones?album=" + elem.getIdAlbum() + "&arti=" + arti.getIdArtista() + "#" + eleme.getNumero() + ">" + eleme.getNumero() + ". " + eleme.getCancion() + "</a><br>");
                }
            }
            out.println("<br><br><br>"); //Saltos de linea obligatorios para correcta vision de pagina junto con la barra de navegacion de letras.
            out.println(Resources.setWordsNav());
            out.println("</div></body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
